﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using SDS.SharePoint.ReplaceDocument.Utility;

namespace SDS.SharePoint.ReplaceDocument.WebControls
{
    /// <summary>
    /// This webcontrol will render the SPFile.Name for use in the TitleArea.
    /// Output uses SharePoint out-of-the-box styles.
    /// </summary>
    public class FileProperty : Microsoft.SharePoint.WebControls.SPControl
    {
        public SPFile File { get; set; }
        public bool ShowParentFolders { get; set; }

        private const string PathSeparator = @"<SPAN><SPAN style=""POSITION: relative; WIDTH: 11px; DISPLAY: inline-block; HEIGHT: 11px; OVERFLOW: hidden"" class=""s4-clust s4-lvtitleseparator""><IMG style=""POSITION: absolute; BORDER-RIGHT-WIDTH: 0px; BORDER-TOP-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px; TOP: -585px !important; LEFT: 0px !important"" alt=: src=""/_layouts/images/fgimg.png""></SPAN></SPAN>";

        protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {
            if (File == null)
                throw new ArgumentNullException();
            if (!File.Exists)
                throw new ArgumentNullException();

            System.Text.StringBuilder sb = new StringBuilder();
            //sb.Append("<h2>");

            if (ShowParentFolders)
            {
                var ParentFolderLinks = File.ParentFolders()
                                            .Select(tParent => String.Format("<a href=\"{0}\">{1}</a>", tParent.ServerRelativeUrl, System.Web.HttpUtility.HtmlEncode(tParent.Name)));

                sb.Append(String.Join(PathSeparator, ParentFolderLinks.ToArray()));
            }

            sb.Append(PathSeparator);
            sb.Append(File.Name);

            //sb.Append("</h2>");
            writer.Write(sb.ToString());

            base.Render(writer);
        } // void Render(...)
    } // class
} // namespace
